package com.appointment.controller.admin;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.appointment.dao.AdminDao;
import com.appointment.dao.ConfirmDao;
import com.appointment.dao.ScoresDao;
import com.appointment.dao.WebDao;
import com.appointment.entity.Admin;
import com.appointment.entity.Web;

@Controller
@RequestMapping("/admin")
public class AdminController {
	
	@Resource
	AdminDao admindao;

	@Resource
	ConfirmDao confirmdao;
	
	@Resource
	ScoresDao scoredao;
	
	@Resource
	WebDao webdao;
	
	@RequestMapping("/tologin")
	public String tologin(){
		return "login/login";
	}
	
	//进入管理信息页面
	@RequestMapping("/tosendMes")
	public String tosendMes(Model model){
		Web web = webdao.findWeb();
		model.addAttribute("web", web);
		return "admin/adminSend";
	}
	
	@RequestMapping("/login")
	public String login(Admin admin,HttpServletRequest request, Model model){
		Admin admins = admindao.findAdminByUsernameAndPsw(admin);
		//如果为空说明用户不存在
		if(admins==null){
			model.addAttribute("message", "用户名密码错误");
			return "login/login";
		}
		
		//如果用户存在将用户信息加入session，进入管理页面
		request.getSession().setAttribute("admin", admin);
		return "redirect:/admin/tohome";
	}
	
	//查询消息的数目
	@RequestMapping("tohome")
	public String tohome(Model model){
		int confirm = confirmdao.findConnum();
		int score = scoredao.findnums();
		model.addAttribute("confirm", confirm);
		model.addAttribute("score", score);
		return "/admin/home";
	}
}
